using System;
using Microsoft.SPOT;
using Gadgeteer.Interfaces.Led;

namespace Gadgeteer.Interfaces.Sample.Safe.SafeApp
{
    /// <summary>
    /// The led should simulate the door:
    /// * Red is closed
    /// * Green is open
    /// The 'door' reacts to the lock events.
    /// </summary>
    public class MultiColorLedAsDoor : IDoor
    {
        public ILock Lock { get; private set; }

        private IMultiColorLed led;

        public MultiColorLedAsDoor(ILock padLock, IMultiColorLed led)
        {
            this.Lock = padLock;
            this.led = led;
            padLock.Opened += padLock_Changed;
            padLock.Closed += padLock_Changed;

            ChangeLed(padLock.IsOpen);
        }

        void padLock_Changed(ILock sender, LockStateEventArgs args)
        {
            ChangeLed(args.IsOpen);
        }

        public void ChangeLed(bool isOpen)
        {
            var color = isOpen ? Color.Green : Color.Red;
            led.TurnColor(color);
        }

        public bool IsOpen
        {
            get { return Lock.IsOpen; }
        }

        public void Close()
        {
            Lock.Close();
        }
    }
}
